﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

using G.Runtime;
using GameDesigner.Source.Widgets;

namespace GameRuntimeControls
{
    public partial class GameStatusControl : UserControl
    {
        private GameSpace _Game;
        private HasGameSpace _HasGame;
        private BoundVariableWidget _NameOfPlayerWithTurnWidget;
        
        public GameStatusControl()
        {
            InitializeComponent();
        }
        
        public HasGameSpace Game
        {
            get { return _HasGame; }
            set
            {
                if (_HasGame != value)
                {
                    _HasGame = value;
                    SetupDisplay();
                }
            }
        }

        void SetupDisplay()
        {
            _Game = _HasGame.Game;
            if (_Game.Players.Count > 1)
            {
                if (_NameOfPlayerWithTurnWidget != null)
                {
                    _NameOfPlayerWithTurnWidget.Dispose();
                }
                _NameOfPlayerWithTurnWidget = new BoundVariableWidget(
                    _Game.Controller.NameOfPlayerWithTurn, 
                    this, new Point(10, 10), "Player");
            }
            else
            {
                _Game.End += OnGameEnd;
            }
        }
        
        private void OnGameEnd(GameEndEventArgs args)
        {
            string message = "You Win";
            if (args.win)
            {
                message = "You Lose";
            }
            new LabelWidget(this, message, new Point(10, 10));
            base.Invalidate();
        }
    }
}
